package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bobomee.android.mentions.edit.util.FormatRangeManager;
import com.bobomee.android.mentions.text.MentionTextView;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.bean.NoticeBean;
import com.dingwei.superiordiary.parser.Parser;
import com.dingwei.superiordiary.ui.activity.FindDetailsActivity;
import com.dingwei.superiordiary.ui.activity.MyCareMeActivity;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.T;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/8.
 */

public class NoticeAdapter extends RecyclerArrayAdapter<NoticeBean.DataBean.ListBean> {
    private FormatRangeManager mRangeManager;
    private Parser mTagParser = new Parser();

    public interface MarkReadInterFace {
        void nickname(int position);

        void ForwardNickName(int position);
        void read(int position);
    }

    private MarkReadInterFace markReadInterFace;

    public void setOnMarkReadInterfaceListener(MarkReadInterFace listener) {
        markReadInterFace = listener;

    }

    public NoticeAdapter(Context context) {
        super(context);
        mRangeManager = new FormatRangeManager();
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new NoticeHolder(parent);
    }

    class NoticeHolder extends BaseViewHolder<NoticeBean.DataBean.ListBean> {


        @Bind(R.id.unread)
        View unread;
        @Bind(R.id.tv_nick_name)
        TextView tvNickName;
        @Bind(R.id.tv_operating)
        TextView tvOperating;
        @Bind(R.id.tv_add_time)
        TextView tvAddTime;
        @Bind(R.id.top_layout)
        LinearLayout topLayout;
        @Bind(R.id.tv_content1)
        TextView tvContent1;
        @Bind(R.id.img_size)
        TextView imgSize;
        @Bind(R.id.img_layout)
        LinearLayout imgLayout;
        @Bind(R.id.img_video)
        ImageView imgVideo;
        @Bind(R.id.tv_forward_nick_name)
        TextView tvForwardNickName;
        @Bind(R.id.tv_add_time1)
        TextView tvAddTime1;
        @Bind(R.id.tv_content2)
        MentionTextView tvContent2;
        @Bind(R.id.img_size1)
        TextView imgSize1;
        @Bind(R.id.img_layout1)
        LinearLayout imgLayout1;
        @Bind(R.id.img_video1)
        ImageView imgVideo1;
        @Bind(R.id.post_layout)
        LinearLayout postLayout;
        @Bind(R.id.content_layout)
        LinearLayout contentLayout;

        public NoticeHolder(ViewGroup parent) {
            super(parent, R.layout.item_notice);
            ButterKnife.bind(this, itemView);

        }

        @Override
        public void setData(final NoticeBean.DataBean.ListBean data) {
            super.setData(data);
            tvContent2.setMovementMethod(new LinkMovementMethod());
            tvContent2.setParserConverter(mTagParser);

            if (data.status == 1) {
                unread.setVisibility(View.GONE);
            } else {
                unread.setVisibility(View.VISIBLE);
            }

            if (data.type.equals("1")) {//@
                postLayout.setVisibility(View.GONE);
                tvNickName.setText(data.consumer.nickname);
                tvOperating.setText("@了你");
                contentLayout.setVisibility(View.VISIBLE);
                tvAddTime.setText(data.addtime);
                tvContent1.setText(data.content);
                if (data.activity_data.data_type == 0) {
                    imgLayout.setVisibility(View.GONE);
                    imgVideo.setVisibility(View.GONE);
                } else if (data.activity_data.data_type == 1) {
                    imgVideo.setVisibility(View.GONE);
                    imgLayout.setVisibility(View.VISIBLE);
                    imgSize.setText(data.activity_data.image_num);
                } else {
                    imgLayout.setVisibility(View.GONE);
                    imgVideo.setVisibility(View.VISIBLE);
                }
            } else if (data.type.equals("2")) {//评论
                postLayout.setVisibility(View.VISIBLE);
                tvNickName.setText(data.consumer.nickname);
                tvOperating.setText("评论了你");
                tvAddTime.setText(data.addtime);
                contentLayout.setVisibility(View.VISIBLE);
                tvContent1.setText(data.content);
                imgLayout.setVisibility(View.GONE);
                imgVideo.setVisibility(View.GONE);

                tvForwardNickName.setText(data.activity_data.nickname);
                tvAddTime1.setText(data.activity_data.addtime);
                if (data.activity_data!=null&&!TextUtils.isEmpty(data.activity_data.content)){
                    String s = data.activity_data.content.replaceAll("'>", "'>@");
                    tvContent2.setText(mRangeManager.getFormatCharSequence(s));
                }else {
                    tvContent2.setText("帖子已删除");
                }
                if (data.activity_data.data_type == 0) {
                    imgLayout1.setVisibility(View.GONE);
                    imgVideo1.setVisibility(View.GONE);
                } else if (data.activity_data.data_type == 1) {
                    imgVideo1.setVisibility(View.GONE);
                    imgLayout1.setVisibility(View.VISIBLE);
                    imgSize1.setText(data.activity_data.image_num);
                } else {
                    imgLayout1.setVisibility(View.GONE);
                    imgVideo1.setVisibility(View.VISIBLE);
                }
            } else if (data.type.equals("3")) {//点赞
                postLayout.setVisibility(View.GONE);
                tvNickName.setText(data.consumer.nickname);
                tvOperating.setText("赞了你");
                tvAddTime.setText(data.addtime);
                contentLayout.setVisibility(View.GONE);
            } else if (data.type.equals("4")) {//关注
                postLayout.setVisibility(View.GONE);
                tvNickName.setText(data.consumer.nickname);
                tvOperating.setText("关注了你");
                tvAddTime.setText(data.addtime);
                contentLayout.setVisibility(View.GONE);
            }

            tvNickName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    markReadInterFace.nickname(getDataPosition());
                }
            });
            topLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    markReadInterFace.read(getDataPosition());
                    if (data.type.equals("4")){
                        Intent intent = new Intent(BaseActivity.context, MyCareMeActivity.class);
                        intent.putExtra("userid", BaseActivity.value.getUserId());
                        BaseActivity.context.startActivity(intent);
                    }else {
                        if (data.activity_data!=null&&!TextUtils.isEmpty(data.activity_data.content)){
                            Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                            intent.putExtra("id", data.data_id);
                            BaseActivity.context.startActivity(intent);
                        }else {
                            T.showShort(BaseActivity.context,"帖子已删除");
                        }


                    }

                }
            });
            contentLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    markReadInterFace.read(getDataPosition());
                    if (data.type.equals("4")){
                        Intent intent = new Intent(BaseActivity.context, MyCareMeActivity.class);
                        intent.putExtra("userid", BaseActivity.value.getUserId());
                        BaseActivity.context.startActivity(intent);
                    }else {
                        if (data.activity_data!=null&&!TextUtils.isEmpty(data.activity_data.content)){
                            Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                            intent.putExtra("id", data.data_id);
                            BaseActivity.context.startActivity(intent);
                        }else {
                            T.showShort(BaseActivity.context,"帖子已删除");
                        }

                    }
                }
            });
            postLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    markReadInterFace.read(getDataPosition());
                    if (data.type.equals("4")){
                        Intent intent = new Intent(BaseActivity.context, MyCareMeActivity.class);
                        intent.putExtra("userid", BaseActivity.value.getUserId());
                        BaseActivity.context.startActivity(intent);
                    }else {
                        if (data.activity_data!=null&&!TextUtils.isEmpty(data.activity_data.content)){
                            Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                            intent.putExtra("id", data.data_id);
                            BaseActivity.context.startActivity(intent);
                        }else {
                            T.showShort(BaseActivity.context,"帖子已删除");
                        }

                    }
                }
            });
            tvContent2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    markReadInterFace.read(getDataPosition());
                    if (data.type.equals("4")){
                        Intent intent = new Intent(BaseActivity.context, MyCareMeActivity.class);
                        intent.putExtra("userid", BaseActivity.value.getUserId());
                        BaseActivity.context.startActivity(intent);
                    }else {
                        if (data.activity_data!=null&&!TextUtils.isEmpty(data.activity_data.content)){
                            Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                            intent.putExtra("id", data.data_id);
                            BaseActivity.context.startActivity(intent);
                        }else {
                            T.showShort(BaseActivity.context,"帖子已删除");
                        }

                    }
                }
            });

        }
    }
}
